/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.expert.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.expert.domain.Expert;
import com.fzhucc.modules.expert.domain.ExpertMsgBox;
import com.fzhucc.modules.expert.domain.ExpertMsgSci;
import com.fzhucc.modules.expert.domain.dto.*;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.expert.repository.ExpertMsgSciRepository;
import com.fzhucc.modules.expert.service.mapstruct.ExpertMsgSciMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-11-22
**/
@Service
@RequiredArgsConstructor
public class ExpertMsgSciService {

    private final ExpertMsgSciRepository expertMsgSciRepository;
    private final ExpertMsgSciMapper expertMsgSciMapper;

    public Map<String,Object> queryAll(ExpertMsgSciQueryCriteria criteria, Pageable pageable){
        Page<ExpertMsgSci> page = expertMsgSciRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(expertMsgSciMapper::toDto));
    }

    public List<ExpertMsgSciDto> queryAll(ExpertMsgSciQueryCriteria criteria){
        return expertMsgSciMapper.toDto(expertMsgSciRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public ExpertMsgSciDto findById(Integer id) {
        ExpertMsgSci expertMsgSci = expertMsgSciRepository.findById(id).orElseGet(ExpertMsgSci::new);
        ValidationUtil.isNull(expertMsgSci.getId(),"ExpertMsgSci","id",id);
        return expertMsgSciMapper.toDto(expertMsgSci);
    }

    @Transactional(rollbackFor = Exception.class)
    public ExpertMsgSciDto create(ExpertMsgSci resources) {
        return expertMsgSciMapper.toDto(expertMsgSciRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(ExpertMsgSci resources) {
        ExpertMsgSci expertMsgSci = expertMsgSciRepository.findById(resources.getId()).orElseGet(ExpertMsgSci::new);
        ValidationUtil.isNull( expertMsgSci.getId(),"ExpertMsgSci","id",resources.getId());
        expertMsgSci.copy(resources);
        expertMsgSciRepository.save(expertMsgSci);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            expertMsgSciRepository.deleteById(id);
        }
    }

    public void download(List<ExpertMsgSciDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ExpertMsgSciDto expertMsgSci : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("被咨询专家姓名", expertMsgSci.getExpertName());
            map.put("咨询内容", expertMsgSci.getContent());
            map.put("联系方式", expertMsgSci.getPhone());
            map.put("咨询时间", expertMsgSci.getConsultationTime());
            map.put("咨询人姓名", expertMsgSci.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public ExpertMsgSciDto seek(ExpertBoxSciSeekDto resources) {
        ExpertMsgSci expertMsgSci = new ExpertMsgSci();
        expertMsgSci.setExpertName(resources.getExpertName());
        expertMsgSci.setPhone(resources.getPhone());
        expertMsgSci.setConsultationTime(new Timestamp(new Date().getTime()));
        expertMsgSci.setContent(resources.getContent());
        expertMsgSci.setName(resources.getName());
        ExpertMsgSci expertMsgSci1 = expertMsgSciRepository.save(expertMsgSci);
        return expertMsgSciMapper.toDto(expertMsgSci1);
    }
}